package com.hnkywl.sts.dao.sys;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.sys.Cd;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.sys.Zdj;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sys
 * 类描述:    菜单dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class ZdjDao extends SimpleHibernateTemplate<Zdj, Long> {

    public Page<Zdj> findAllZdjList( String pmName,Date start,Date endDate,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Zdj> page = new Page<Zdj>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (start != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated >= ? ");
            param.add(start);
        }
        if (endDate != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated < ? ");
            param.add(endDate);
        }
        if (pmName != null && !pmName.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" pm.name like ? ");
            param.add("%" + pmName.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Zdj where deleted != 1  " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Zdj where deleted != 1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql +" order by drrq desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Zdj findZdjByPmAndDrrq(Long pmId,Date today){
        Date minDate = convertDate(today,true);
        Date maxDate = convertDate(today,false);
        String hql = " from Zdj where drrq < ? and drrq >= ? and pm.id= ? and deleted = 0 ";
        return  find(hql,maxDate,minDate,pmId).size()==0?null:(Zdj)find(hql,maxDate,minDate,pmId).get(0);
    }

    public List<Zdj> findZdjByDrrq(Date drrq){
        Date minDate = convertDate(drrq,true);
        Date maxDate = convertDate(drrq,false);
        return find("from Zdj where drrq < ? and drrq >= ?  ",maxDate,minDate);
    }


    public List<Zdj> findwscZdjByDrrq(Date drrq){
        Date minDate = convertDate(drrq,true);
        Date maxDate = convertDate(drrq,false);
        return find("from Zdj where drrq < ? and drrq >= ? and deleted = 0 ",maxDate,minDate);
    }

    /**
     * @param date
     * @param flag true:当天00:00:00   false:第二天00:00:00
     * @return
     */
    private Date convertDate(Date date, boolean flag) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        if (!flag)
            gc.add(GregorianCalendar.DAY_OF_MONTH, 1);
        gc.set(GregorianCalendar.HOUR, 0);
        gc.set(GregorianCalendar.MINUTE, 0);
        gc.set(GregorianCalendar.SECOND, 0);
        return gc.getTime();
    }

}
